local skel = fk.CreateSkill {
  name = "emo__shenwen",
}

skel:addEffect(fk.EnterDying, {
  anim_type = "offensive",
  events = {fk.EnterDying},
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(skel.name) and player ~= target and player.phase ~= Player.NotActive and not target.dead
  end,
  on_cost = function (self, event, target, player, data)
    if player.room:askToSkillInvoke(player, {skill_name =  skel.name, prompt = "#emo__shenwen-invoke:"..target.id}) then
      event:setCostData(self, {tos = {target}})
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local choices = {"#emo__shenwen_true", "#emo__shenwen_false"}
    local guess = room:askToChoice(player, {
      choices = choices, skill_name = skel.name, prompt = "#emo__shenwen-choice:"..target.id,
    }) == choices[1]
    local has = table.find(target:getCardIds("h"), function (id)
      return Fk:getCardById(id).name == "peach" or Fk:getCardById(id).name == "analeptic"
    end) ~= nil
    if not target.dead then
      local card = room:askToChooseCard(player, {
        target = target, skill_name = skel.name, prompt = "#emo__shenwen-throw:"..target.id,
        flag = { card_data = { { "$Hand", target.player_cards[Player.Hand] } } }
      })
      room:throwCard(card, skel.name, target, player)
    end
    if guess == has then
      data.extra_data = data.extra_data or {}
      data.extra_data.emo__shenwen_from = player.id
    end
  end,
})

skel:addEffect(fk.AfterDying, {
  is_delay_effect = true,
  anim_type = "offensive",
  can_trigger = function(self, event, target, player, data)
    return data.extra_data and data.extra_data.emo__shenwen_from == player.id and data.extra_data.emo__shenwen_victims
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    player:broadcastSkillInvoke("emo__shenwen")
    room:delay(600)
    local tos = data.extra_data.emo__shenwen_victims
    room:sortByAction(tos)
    for _, to in ipairs(tos) do
      if not to.dead then
        room:doIndicate(player, {target})
        room:damage { from = player, to = to, damage = 1, skillName = skel.name }
      end
    end
  end,
})

-- 不能用事件查找器吗
skel:addEffect(fk.HpRecover, {
  can_refresh = function (self, event, target, player, data)
    if player == data.recoverBy then
      local e = player.room.logic:getCurrentEvent():findParent(GameEvent.Dying)
      if e then
        local dying = e.data
        return dying.extra_data and dying.extra_data.emo__shenwen_from and target == dying.who
      end
    end
  end,
  on_refresh = function (self, event, target, player, data)
    local e = player.room.logic:getCurrentEvent():findParent(GameEvent.Dying)
    if e then
      local dying = e.data
      local tos = dying.extra_data.emo__shenwen_victims or {}
      table.insertIfNeed(tos, player)
      dying.extra_data.emo__shenwen_victims = tos
    end
  end,
})


Fk:loadTranslationTable{
  ["emo__shenwen"] = "审问",
  [":emo__shenwen"] = "当一名其他角色于你的回合内进入濒死状态时，你可以猜测该角色手牌中是否有【桃】或【酒】，然后观看其手牌并弃置其中一张，若你猜对，你于濒死结算后对本次结算中令其回复体力的角色各造成1点伤害。",
  ["#emo__shenwen-invoke"] = "你可以“审问” %src ！猜测手牌中是否有【桃】或【酒】",
  ["#emo__shenwen-throw"] = "审问：观看 %src 手牌并弃置一张",
  ["#emo__shenwen_delay"] = "审问",
  ["#emo__shenwen-choice"] = "审问：猜测 %src 手牌中是否有【桃】或【酒】",
  ["#emo__shenwen_true"] = "有！",
  ["#emo__shenwen_false"] = "没有",

  ["$emo__shenwen1"] = "You're mine, palhaço.",
  ["$emo__shenwen2"] = "You're all mine, seu miserável!",
}


return skel
